#include "listitem.h"
#include "ui_listitem.h"
#include <QDebug>
#include <QMessageBox>

listitem::listitem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::listitem)
{
    ui->setupUi(this);
}

listitem::~listitem()
{
    delete ui;
}

///更新一项的信息
void listitem::updateItemInfo(const QVector<QString> PhoneInfo,QString JoinNumber)
{
//    qDebug() << PhoneInfo;


    QString PictureName = "../Phone_platform/sources/picture" + PhoneInfo.at(4) + "001.jpg";

    ui->lb_picture->setPixmap(QPixmap::fromImage(QImage(PictureName)));
    ui->lb_modelnumber->setText(PhoneInfo.value(0));
    ui->lb_price->setText(PhoneInfo.value(1));
    ui->lb_SingalNum->setText(QString::number(PhoneInfo.value(1).toInt()*JoinNumber.toInt()));
    ui->lb_Number->setText(JoinNumber);
//    qDebug() << ShoppingNumber;
}

//数量加
void listitem::on_pb_AddNumber_clicked()
{
    int number = ui->lb_Number->text().toInt();
    int SingalPrice = ui->lb_price->text().toInt();

    ui->lb_SingalNum->setText(QString::number((number+1)*SingalPrice));
    ui->lb_Number->setText(QString::number(number+1));
    emit SignalAfterModifyNumber(ui->lb_modelnumber->text(),QString::number(number+1));
}


//数量减
void listitem::on_pb_CutNumber_clicked()
{
    int number = ui->lb_Number->text().toInt();
    int SingalPrice = ui->lb_price->text().toInt();

    if(number == 1)
    {
        ui->lb_SingalNum->setText(ui->lb_price->text());
        QMessageBox::warning(this, tr("提示："),tr("客官，不能再减了哦~"));
    }
    else
    {
        ui->lb_Number->setText(QString::number(number-1));
        ui->lb_SingalNum->setText(QString::number((number-1)*SingalPrice));
        emit SignalAfterModifyNumber(ui->lb_modelnumber->text(),QString::number(number-1));
    }
}


///从购物车移除商品按钮
void listitem::on_pb_RemoveFoods_clicked()
{
    ////未实现////
//    ///移除前判断是否计入合计 || 如果是则总合计中减去
//    if(ui->cb_join->isChecked())//选中
//    {
//        qDebug() << "是选中的";
//        emit Signal_IsJoinNum(false,ui->lb_SingalNum->text());
//    }
//    else if(ui->cb_join->isChecked())//未选中
//    {
//        //不做处理
//    }
//    qDebug() << "移除成功！";
    emit SignalRemoveFoods(ui->lb_modelnumber->text());///从数据库中删除信息
//    this->hide();
}


///是否加入合计
void listitem::on_cb_join_stateChanged(int arg1)
{
    if(ui->cb_join->isChecked())
    {
        ui->pb_AddNumber->setDisabled(true);
        ui->pb_CutNumber->setDisabled(true);
        ui->pb_RemoveFoods->setDisabled(true);
    }
    else if(ui->cb_join->isCheckable())
    {
        ui->pb_AddNumber->setDisabled(false);
        ui->pb_CutNumber->setDisabled(false);
        ui->pb_RemoveFoods->setDisabled(false);
    }

//    qDebug() << ui->cb_join->isChecked() << "++++++++++++++++" << ui->lb_SingalNum->text();
    emit Signal_IsJoinNum(ui->cb_join->isChecked(),ui->lb_SingalNum->text());
}
